<script setup>
import $bus from "../../utils/bus";
import { onMounted, ref } from "vue";
import { ElMessage } from "element-plus";
import { getPageTradeStockApi } from "@/api/tradeReport";
import { userUserStore } from "@/stores/user";
import {baseUrl} from '@/utils/constont'
// ---------------------------------------------------------------------获取查询刷新---------------------------------------------------------------------
const dataList = ref([]);
const queryPageParams = ref({
  pageNum: 1,
  pageSize: 10,
  goodsName: "",
  dateTime: [
    new Date().toISOString().split("T")[0], // 今天
    new Date().toISOString().split("T")[0], // 今天
  ],
});

const total = ref(0);
const getData = async () => {
  console.log(queryPageParams.value);
  const res = await getPageTradeStockApi(queryPageParams.value);
  dataList.value = res.data.list;
  total.value = res.data.total;
};
const getPage = () => {
  getData();
};
const clear = () => {
  queryPageParams.value.pageNum = 1;
  queryPageParams.value.pageSize = 10;
  queryPageParams.value.goodsName = "";
  queryPageParams.dateTime = [
    new Date().toISOString().split("T")[0], // 今天
    new Date().toISOString().split("T")[0], // 今天
  ],
  getData();
};
const handlePageChange = () => {
  getData();
};
//---------------------------------------------------------------------导入---------------------------------------------------------------------
const formRef = ref(null);
const open = ref(false);
const title = ref("上传");
const uploadUrl = ref(baseUrl+"/trade/tradeStock/upload")
const form = ref({
  dateTime: new Date().toISOString().split("T")[0], // 今天
});
const upload = () => {
  open.value = true;
};
const closeDialog = () => {
  open.value = false;
  form.value.dateTime = new Date().toISOString().split("T")[0];
};
const uploadRef = ref(null);
const userStore = userUserStore();
const headobj = ref({
  fuge_token: userStore.userInfo.token,
});
const beforeUpload = (file) => {

  if (file.size / 1024 / 1024 > 10) {
    ElMessage.error("文件大小不能超过10MB!");
    return false;
  }
  return true;
};
const uploadParams = ref({
  start: form.value.dateTime,
  end: form.value.dateTime
})
const errorUpload = (response) => {
  console.log(response);
  if (response.status === 500) {
    ElMessage({
      message: "传入失败请联系管理员",
      type: "error",
    });
  } else {
    ElMessage({
      message: "登入到期,请重新登入",
      type: "error",
    });
    userStore.clearUserInfo();
    router.push("/login");
  }
};
const successUpload = (response) => {
  console.log(response);
  if (response.code != 200) {
    ElMessage({
      message: response.errorMessage,
      type: "error",
    });
  } else if (response.code == 200) {
    ElMessage({
      message: response.data,
      type: "success",
    });
    open.value = false
  }
};
//---------------------------------------------------------------------获取标题事件---------------------------------------------------------------------
const titleData = ref({
  titleFir: "贸易数据",
  titleSec: "报表数据",
});
onMounted(() => {
  $bus.emit("title-info", titleData.value);
  getData();
});
</script>
<template>
  <el-card>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="商品名称">
        <el-input placeholder="请输入商品名称" v-model="queryPageParams.customerName" />
      </el-form-item>

      <el-form-item label="收货时间">
        <el-date-picker
          v-model="queryPageParams.dateTime"
          type="daterange"
          range-separator="To"
          start-placeholder="Start date"
          end-placeholder="End date"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>

      <el-form-item>
        <el-button icon="Search" type="primary" @click="getPage()">查询</el-button>
        <el-button icon="RefreshRight" type="info" @click="clear()">重置</el-button>
        <el-button icon="Edit" type="danger" @click="upload()">导入</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card>
    <el-table :data="dataList" stripe fit>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column width="55" type="index" label="序号"></el-table-column>
      <el-table-column label="商户名称" prop="id"></el-table-column>
    </el-table>
    <el-pagination v-model:current-page="queryPageParams.pageNum" v-model:page-size="queryPageParams.pageSize"
      :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="+total"
      @change="handlePageChange" />
  </el-card>

  <el-dialog
    v-model="open"
    :title="title"
    width="50%"
    :before-close="closeDialog"
    draggable
  >
    <el-form :model="form" label-width="auto" ref="formRef">
      <el-form-item label="时间">
        <el-date-picker
          v-model="form.dateTime"
          type="date"
          placeholder="请选择日期"
          size="large"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <!-- <el-button @click="closeDialog()">取消</el-button> -->

        <el-upload
          ref="uploadRef"
          class="upload-demo"
          :data="uploadParams"
          :headers="headobj"
          :before-upload="beforeUpload"
          :on-success="successUpload"
          :on-error="errorUpload"
          style="width: 100%"
          :action="uploadUrl"
        >
          <template #trigger>
            <el-button type="primary">上传</el-button>
          </template>
        </el-upload>

      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss"></style>
